package chapter2;


public class Body {
    private long idNum;
    private String name;
    private Body orbits;
    
    private static long nextID = 0;
    
    {
        System.out.println("init block");
        idNum = nextID++;
        
        //not all constructor throw exception, have to try/catch
        //FileInputStream fs = new FileInputStream("");
        //fs.read();
    }
    
    protected Body() {
        System.out.println("default constructor");
        idNum = nextID++;
    }
    

    
    Body(String bodyName) {
        //this(bodyName,this.orbits);//Cannot refer to 'this' nor 'super' while explicitly invoking a constructor
        this(bodyName,null);
    }
    
    Body(String bodyName, Body orbitsAround) {
        this();
        name = bodyName;
        orbits = orbitsAround;
    }
    
    public String toString(){
        String desc = idNum + " (" + name + ")";
        if(orbits != null){
            desc += " orbits " + orbits.toString();
        }
        return desc;
    }
    
    public void setOrbiters(Body... bodies){
        if(bodies != null){
            System.out.println("setOrbiters " + bodies.length);
        }else{
            System.out.println("setOrbiters null bodies");
        }
    }
    
    public void capture(Body victim){
        //access control is per-class not per-object
        victim.orbits = this;
    }
    
    public static void main(String[] args){
        Body sun = new Body("Sol", null);
        Body earth = new Body("Earth", sun);
        earth.setOrbiters();//empty array
        earth.setOrbiters(null);//null
        earth.setOrbiters((Body[])null);//null
        earth.setOrbiters((Body)null);//an array {null}
        
        System.out.println(sun);
        System.out.println(earth);
    }



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }
    

}